package view.panels;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ListSelectionEvent;
import menu.commands.Option;
import util.reflection.Reflection;
import util.resources.ResourceManager;
import buttons.AbstractCommandButton;
import buttons.RadioButtonGroup;
import controller.TurtleController;

public class OptionList extends AbstractListPanel<String>
{
    private Map<String, RadioButtonGroup> myOptionMap;
    private Option myOption;

    public OptionList(TurtleController controller)
    {
        super(controller);
        myOptionMap = createRadioGroupMap();
        myOption = new Option();
        List<String> list = new ArrayList<String>();
        list.addAll(myOptionMap.keySet());
        this.replaceList(list);
    }

    private Map<String, RadioButtonGroup> createRadioGroupMap()
    {
        ResourceManager resources = ResourceManager.getInstance();
        resources.addResourcesFromFile("option");
        Map<String, RadioButtonGroup> radioGroupMap = new HashMap<String, RadioButtonGroup>();
        Map<String, List<JRadioButton>> radioMap = new HashMap<String, List<JRadioButton>>();

        String[] categories = resources.getStringArray("Options");
        for (String category : categories)
        {
            String[] options = resources.getStringArray(category);
            for (String option : options)
            {
                String[] radioButtonProperty = resources.getStringArray(option);
                String className = radioButtonProperty[0];
                String label = radioButtonProperty[1];
                AbstractCommandButton<?> buttonInstance = (AbstractCommandButton<?>) Reflection
                        .createInstance(className, label);
                if (radioMap.get(option) == null)
                {
                    radioMap.put(option, new ArrayList<JRadioButton>());
                }
                radioMap.get(option).add(buttonInstance);
            }
            for (List<JRadioButton> radiobutton : radioMap.values())
            {
                if (radioGroupMap.get(category) == null)
                {
                    radioGroupMap.put(category, new RadioButtonGroup());
                }
                radioGroupMap.get(category).addAll(radiobutton);
            }
        }
        return radioGroupMap;
    }

    @Override
    public void eventOccurred(ListSelectionEvent event)
    {
        if (!event.getValueIsAdjusting())
        {
            int whichItem = getIndexOfFirstSelectedItem();
            if (whichItem != -1)
            {// else, the event isn't the kind of event we're looking for
                String option = getListItem(whichItem);
                JPanel panel = myOptionMap.get(option).getInstance();
                myOption.replacePane(panel);
                clearListSelection();
            }
        }

    }
}
